<?php
/**
 * @Project: None
 * @FileName: Game.php
 * @FilePath: D:\thinkphp6\tp\app\controller\Game.php
 * @Author: zishuiliufeng
 * @Date:   2021-01-28 17:21:49
 * @Last Modified by:   zishuiliufeng
 * @Last Modified time: 2021-02-06 17:59:53
 */
namespace app\controller;
use think\Facade\Db;
use app\model\Game as GameModel;
use think\facade\Request;
use app\BaseController;
use app\Response;

class Game extends BaseController{
    /**
     * [gameList 获取游戏列表]
     * @return [type] [description]
     */
    public function gameList(){
        //静态获取参数
        //$param = Request::param();
        $param = $this->request->param();
        $page = isset($param['page'])?(int)$param['page']:1;
        $perpage = isset($param['perpage'])?(int)$param['perpage']:5;

        $name = isset($param['name'])?(string)$param['name']:'';

        //筛选条件数组
        $where = [];

        if(!empty($name)){
            $where[] = [
                ['name', 'like', '%'.$name.'%']
            ];
        }

        //$game = Db::name('game')->where('id','=',1)->select();
        //$game = GameModel::where('id','=',1)->select();
        //var_dump($game);
        // 查询状态为1的用户数据 并且每页显示10条数据
        $list = Db::name('game')->where($where)->order('id' ,'asc')->paginate($perpage);
        //当前页数
        $currentPage = $list->currentPage();
        // 获取总记录数
        $total = $list->total();
        //最后页数
        $lastPage = $list->lastPage();
        //每页显示数
        $listRows = $list->listRows();
        //数据
        $data = $list->items();
        
        $result = array(
            'currentPage' => $currentPage,
            'total' => $total,
            'lastPage' => $lastPage,
            'listRows' => $listRows,
            'content' => $data
        );

        
        Response::show(200,'ok',$result);
    }
    /**
     * [edit 修改游戏信息]
     * @return [type] [description]
     */
    public function edit(){
        $param = $this->request->param();
        $id = isset($param['id'])?(int)$param['id']:0;
        if($id < 1){
            $result = array('id'=>$id);
            Response::show(201,'参数错误',$result);
        }

        $name = isset($param['name'])?trim((string)$param['name']):'';
        //游戏名称不能为空
        if(empty($name)){
            Response::show(301,'游戏名称不能为空');
        }

        $introduction = isset($param['introduction'])?trim((string)$param['introduction']):'';
        $path = isset($param['path'])?trim((string)$param['path']):'';

        //游戏名称不能相同
        $repeat = GameModel::where('name',$name)->findOrEmpty();
        if(!$repeat->isEmpty() && $repeat->id != $id){
            Response::show(300,'游戏名称已存在');
        }

        //根据id查找数据，并判断数据是否存在
        $game = GameModel::where('id',$id)->findOrEmpty();
        if($game->isEmpty()){
            Response::show(404,'数据不存在');
        }
        //存在则修改
        $game->name = $name;
        $game->introduction = $introduction;
        $game->path = $path;
        $game->update_time = date('Y-m-d H:i:s');
        $game->save();
        Response::show(200,'修改成功');
    }

    /**
     * [addGame 新增游戏]
     */
    public function addGame(){
        $param = $this->request->param();
        $name = isset($param['name'])?(string)$param['name']:'';
        $introduction = isset($param['introduction'])?(string)$param['introduction']:'';
        $path = isset($param['path'])?(string)$param['path']:'';

        //游戏名称不能为空
        if(empty($name)){
            Response::show(301,'游戏名称不能为空');
        }

        //游戏名称不能相同
        $repeat = GameModel::where('name',$name)->findOrEmpty();
        if(!$repeat->isEmpty()){
            Response::show(300,'游戏名称已存在');
        }

        $game = new GameModel;
        $game->name = $name;
        $game->introduction = $introduction;
        $game->path = $path;
        $game->create_time = date('Y-m-d H:i:s');
        $game->update_time = date('Y-m-d H:i:s');
        $game->save();
        //获取自增ID
        $id = $game->id;
        Response::show(200,'新增成功',$id);
    }
    /**
     * [delGame 删除游戏]
     * @return [type] [json]
     */
    public function delGame()
    {
        $param = $this->request->param();
        $id = isset($param['id'])?(int)$param['id']:0;
        //验证游戏是否存在
        $event = GameModel::where('id',$id)->findOrEmpty();
        if($event->isEmpty()){
            Response::show(404,'游戏不存在');
        }

        $del = $event->delete();    //bool事件，删除成功返回true,失败返回false
        if($del){
            $code = 200;
            $msg = '删除成功';
        }else{
            $code = 204;
            $msg = '删除失败';
        }

        Response::show($code,$msg);
    }
}